package com.supor.aiot.mine.ui.mine;

import android.annotation.SuppressLint;
import android.app.Application;

import com.android.baseconfig.base.BaseApiResult;
import com.android.baseconfig.base.RxBus3;
import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.app.SuporPresenter;
import com.supor.aiot.common.app.api.UserImpl;
import com.supor.aiot.common.base.BaseViewModel;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.entity.UserInfo;
import com.supor.aiot.md_msg.api.MsgCenterApiImpl;
import com.supor.aiot.md_msg.entity.MsgCenterListResultBean;

import java.util.HashMap;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import autodispose2.AutoDisposeConverter;
import io.reactivex.rxjava3.functions.Consumer;

public class MineViewModel extends BaseViewModel {

    private MutableLiveData<UserInfo> userInfo = new MutableLiveData<>();
    private MutableLiveData<Result<MsgCenterListResultBean>> msgUnReadResult = new MutableLiveData<>();
    private MutableLiveData<Result> updateResult = new MutableLiveData<>();
    private MutableLiveData<String> userRankResult = new MutableLiveData<>();

    public MineViewModel(@NonNull Application application) {
        super(application);
    }

    public MutableLiveData<UserInfo> getUserInfo() {
        return userInfo;
    }

    public MutableLiveData<Result> getUpdateResult() {
        return updateResult;
    }

    public MutableLiveData<String> getUserRankResult() {
        return userRankResult;
    }

    @SuppressLint("CheckResult")
    public void updateUser(UserInfo userInfo) {
        Map<String, String> map = new HashMap<>();
        map.put("providerId", "0");
        map.put("bossUserId", userInfo.getBossUserId());
        map.put("sign", userInfo.getSign() == null ? "" : userInfo.getSign());
        UserImpl.getInstance().updateUser(userInfo.getBossUserId(), map).subscribe(apiResult -> {
            if (apiResult.isSuccess()) {
                // 更新用户成功后重新获取下用户信息
                getUser(userInfo.getBossUserId());
            } else {
                updateResult.setValue(new Result.Error("updateUser failed"));
            }
        }, throwable -> {
            Logc.e("updateUser: " + throwable);
            updateResult.setValue(new Result.Error(new Exception(throwable)));
        });
    }

    /**
     * 获取用户信息
     *
     * @param id 用户ID
     */
    @SuppressLint("CheckResult")
    public void getUser(String id) {
        getUser(id, true);
    }

    /**
     * 获取用户信息
     *
     * @param id 用户ID
     * @param needToast 更新完成是否需要提示
     */
    @SuppressLint("CheckResult")
    public void getUser(String id, boolean needToast) {
        UserImpl.getInstance().getUser(id).subscribe(apiResult -> {
            if (apiResult.isSuccess()) {
                SuporPresenter.getInstance().saveInfo(apiResult.data, false);
                RxBus3.getInstance().post(Constants.Tag.LOGIN, Constants.Value.SUCCESS);
                if (needToast) {
                    updateResult.setValue(new Result.Success(apiResult.data));
                }
            } else {
                if (needToast) {
                    updateResult.setValue(new Result.Error("getUser failed"));
                }
            }
        }, throwable -> {
            Logc.e("getUser: " + throwable);
            if (needToast) {
                updateResult.setValue(new Result.Error(new Exception(throwable)));
            }
        });
    }

    /**
     * 获取用户会员等级
     */
    @SuppressLint("CheckResult")
    public void getUserRank() {
        UserImpl.getInstance().getUserRank().subscribe(apiResult -> {
            if (apiResult.isSuccess()) {
                userRankResult.setValue(apiResult.data);
            } else {
                userRankResult.setValue("getUser failed");
            }
        }, throwable -> {
            Logc.e("getUserRank: " + throwable);
            userRankResult.setValue(throwable.getMessage());
        });
    }

    public void getSystemMsgListCount(String bossUserId,
                                      AutoDisposeConverter<BaseApiResult<MsgCenterListResultBean>> autoDisposeConverter
    ) {
        MsgCenterApiImpl.getInstance().getMsgListUnReadCount(bossUserId,
                "SYSTEM",
                "CREATE_TIME%20DESC",
                0,
                100, 1)
                .to(autoDisposeConverter)
                .subscribe(new Consumer<BaseApiResult<MsgCenterListResultBean>>() {
                    @Override
                    public void accept(BaseApiResult<MsgCenterListResultBean> msgCenterListBeanBaseApiResult) throws Exception {
                        msgUnReadResult.setValue(new Result.Success<>(msgCenterListBeanBaseApiResult.data));
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        msgUnReadResult.setValue(new Result.Error(new Exception(throwable)));
                    }
                });
    }

    public MutableLiveData<Result<MsgCenterListResultBean>> getMsgUnReadResult() {
        return msgUnReadResult;
    }
}
